<?php

namespace app\api\controller;


use app\api\model\Holidays;
use app\api\model\house\HouseMedia;
use app\common\controller\Api;

use app\api\model\house\HouseCollect as HouseCollectModel;
use app\api\model\house\House as HouseModel;

/**
 * 房屋收藏接口
 */
class Housecollect extends Api
{

    protected $noNeedRight = ['*'];

    //收藏
    public function add()
    {
        $house_id = input('house_id',0);
        if (empty($house_id)) {
            $this->error('缺少参数房屋id');
        }
        $data = [
            'user_id' => $this->auth->id,
            'house_id' => $house_id,
        ];

        $collect = new HouseCollectModel();

        $info = HouseCollectModel::where(['house_id'=>$house_id, 'user_id' => $this->auth->id])->find();
        if (empty($info)){
            $res = $collect->save($data);
            if ($res){
                HouseModel::where(['house_id'=>$house_id])->setInc('collect_num',1);
            }
            $this->success('收藏成功',$res);
        } else {
            $this->error('收藏失败');
        }
    }

    //删除
    public function delete()
    {
        $house_id = input('house_id',0);
        if (empty($house_id)) {
            $this->error('缺少参数房屋id');
        }

        $info = HouseCollectModel::where(['house_id'=>$house_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)){
            $res = $info->delete();
            if ($res){
                HouseModel::where(['house_id'=>$house_id])->setDec('collect_num',1);
            }
            $this->success('取消成功',$res);
        } else {
            $this->error('取消失败');
        }
    }

    //是否收藏
    public function iscollect()
    {
        $house_id = input('house_id',0);
        if (empty($house_id)) {
            $this->error('缺少参数房屋id');
        }

        $info = HouseCollectModel::where(['house_id'=>$house_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)) {
            $this->success('',1);
        } else {
            $this->success('',0);
        }
    }

    //收藏列表
    public function lists()
    {
        $house_model = new HouseModel();
        $house_level_list = $house_model->getHouseLevelList();

        $list = HouseCollectModel::alias('hc')
            ->join('house h','hc.house_id = h.house_id')
            ->where(['hc.user_id'=>$this->auth->id])
            ->field('hc.createtime,h.house_id,h.house_title,h.score,h.house_attr,h.house_level,h.mode,h.share_weekday_price,
            h.share_weekend_price,h.share_highday_price,h.is_group,h.comment_num,h.house_facilities_id,h.group_weekday_price,
            h.group_weekend_price,h.group_highday_price,h.describe_score,h.location_score,h.convenient_score,h.service_score,
            h.health_score,h.cost_performance_score,collect_num')
            ->order('hc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $holidays = Holidays::where(['type'=>0])->column('date');
        $workdays = Holidays::where(['type'=>1])->column('date');

        $date = date('Y-m-d');
        $day_of_week = date('N');

        foreach ($list as $k=>$v){
            $media = HouseMedia::where(['house_id'=>$v['house_id']])
                ->order('createtime asc')->column('media');
            $list[$k]['cover'] = $media[0];

            if (in_array($date, $holidays)) {
                $list[$k]['today_price'] = $v['share_highday_price'];
                $group_price_arr = $v['group_highday_price'];
                //continue;
            } else {
                if ($day_of_week == 6 || $day_of_week == 7) {
                    if (!in_array($date, $workdays)){
                        $list[$k]['today_price'] = $v['share_weekend_price'];
                        $group_price_arr = $v['group_weekend_price'];
                    } else {
                        $list[$k]['today_price'] = $v['share_weekday_price'];
                        $group_price_arr = $v['group_weekday_price'];
                    }
                    //continue;
                } else {
                    $list[$k]['today_price'] = $v['share_weekday_price'];
                    $group_price_arr = $v['group_weekday_price'];
                }
            }
            $price_arr = [$v['share_highday_price'],$v['share_weekend_price'],$v['share_weekday_price']];
            $list[$k]['mark_price'] = max($price_arr);
            $group_price_arr = json_decode($group_price_arr,true);
            foreach ($group_price_arr as $kk=>$vv){
                if ($vv['type']=='all'){
                    $group_price_arr[$kk]['title'] = '整房';
                } else {
                    $group_price_arr[$kk]['title'] = '卧室'.$vv['type'];
                }
            }
            $list[$k]['group_price'] = $group_price_arr;
            $list[$k]['house_level_text'] = $house_level_list[$v['house_level']];
            $mode_arr = [];
            $mode = explode(',',$v['mode']);
            foreach ($mode as $kk=>$vv){
                if ($vv==2||$vv==4){
                    if ($v['is_group']==1){
                        array_push($mode_arr,'拼房');
                    }
                } elseif ($vv==3){
                    array_push($mode_arr,'换房');
                }
            }
            $list[$k]['mode_text'] = array_unique($mode_arr);
        }
        $this->success('',$list);
    }

    //收藏排名
    public function ranking()
    {
        $list = HouseModel::field('house_id,house_title,score,house_attr,house_level,mode,share_weekday_price,share_weekend_price,share_highday_price,
        is_group,comment_num,house_facilities_id,group_weekday_price,group_weekend_price,group_highday_price,describe_score,location_score,
        convenient_score,service_score,health_score,cost_performance_score,collect_num')
            ->order('collect_num desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $holidays = Holidays::where(['type'=>0])->column('date');
        $workdays = Holidays::where(['type'=>1])->column('date');

        $date = date('Y-m-d');
        $day_of_week = date('N');

        foreach ($list as $k=>$v){
            $media = HouseMedia::where(['house_id'=>$v['house_id']])
                ->order('createtime asc')->column('media');
            $list[$k]['cover'] = $media[0];

            if (in_array($date, $holidays)) {
                $list[$k]['today_price'] = $v['share_highday_price'];
                $group_price_arr = $v['group_highday_price'];
                //continue;
            } else {
                if ($day_of_week == 6 || $day_of_week == 7) {
                    if (!in_array($date, $workdays)){
                        $list[$k]['today_price'] = $v['share_weekend_price'];
                        $group_price_arr = $v['group_weekend_price'];
                    } else {
                        $list[$k]['today_price'] = $v['share_weekday_price'];
                        $group_price_arr = $v['group_weekday_price'];
                    }
                    //continue;
                } else {
                    $list[$k]['today_price'] = $v['share_weekday_price'];
                    $group_price_arr = $v['group_weekday_price'];
                }
            }
            $price_arr = [$v['share_highday_price'],$v['share_weekend_price'],$v['share_weekday_price']];
            $list[$k]['mark_price'] = max($price_arr);
            $group_price_arr = json_decode($group_price_arr,true);
            foreach ($group_price_arr as $kk=>$vv){
                if ($vv['type']=='all'){
                    $group_price_arr[$kk]['title'] = '整房';
                } else {
                    $group_price_arr[$kk]['title'] = '卧室'.$vv['type'];
                }
            }
            $list[$k]['group_price'] = $group_price_arr;
        }

        $this->success('',$list);
    }

    //房源的收藏列表
    public function collectList()
    {
        $house_id = input('house_id',0);

        $list = HouseCollectModel::alias('hc')
            ->join('user u','hc.user_id = u.id')
            ->where(['hc.user_id'=>$this->auth->id,'hc.house_id'=>$house_id])
            ->field('u.nickname,u.avatar,hc.createtime')
            ->order('hc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);
    }
}